package com.kuzlak.cms.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuzlak.cms.bean.MyPage;
import com.kuzlak.cms.module.sys.entity.SysSettings;
import com.kuzlak.cms.module.sys.mapper.SysSettingsMapper;
import com.kuzlak.cms.module.sys.service.SysSettingsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
* @author hesenjan
* @description 针对表【sys_settings】的数据库操作Service实现
* @createDate 2024-11-30 23:46:05
*/
@Service
public class SysSettingsServiceImpl extends ServiceImpl<SysSettingsMapper, SysSettings>
    implements SysSettingsService{

    @Override
    public boolean deleteById(long id) {

        int i = this.baseMapper.deleteById(id);
        return (i != 0);
    }

    @Override
    public MyPage findByPage(int pagesize, int page, String name, int status, String isKey) {
        QueryWrapper<SysSettings> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysSettings::getDeleted, 0);
        queryWrapper.orderByDesc("ID");
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.lambda().likeRight(SysSettings::getName, name);
        }


        if (StringUtils.isNotEmpty(isKey)) {
            queryWrapper.lambda().likeRight(SysSettings::getIsKey, isKey);
        }
        if (status > 0) {
            queryWrapper.lambda().eq(SysSettings::getStatus, status);
        }
        Page<SysSettings> page0 = new Page<>(page, pagesize);
        IPage<SysSettings> data = this.baseMapper.selectPage(page0, queryWrapper);

     return  MyPage.parsePage(data);
    }

    @Cacheable(cacheNames = "settings", key = "'findByKeys'")
    @Override
    public Map<String, Object> findByKeys() {

        Map<String, Object> map = new HashMap<>();
        List<SysSettings> list = this.list();
        Iterator<SysSettings> iterator = list.iterator();
        while (iterator.hasNext()) {
            SysSettings next = iterator.next();
            map.put(next.getIsKey(), next.getIsValue());
        }
        return map;
    }

}






